package com.example.fengshoutong.ui;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.example.fengshoutong.MainApplication;
import com.example.fengshoutong.R;
import com.example.fengshoutong.bean.Article;
import com.example.fengshoutong.greendao.ArticleDao;
import com.example.fengshoutong.utils.Constant;

/**
 * 文章页面
 */
public class ArticleFragment extends Fragment {
    // 顶部标题
    private TextView text_top_title;
    // 大标题
    private TextView text_title;
    // 文章内容
    private TextView text_content;
    // 文章数据库
    private ArticleDao articleDao;
    // 当前文章
    private Article article;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_article, container, false);
    }

    @Override
    public void onResume() {
        super.onResume();
        // 获取文章数据库
        articleDao = MainApplication.getInstance().getArticleDaoSession().getArticleDao();
        article = Constant.currentArticle;
        initView();
        addViews();
    }

    private void initView() {
        text_top_title = getView().findViewById(R.id.text_top_title);
        text_title = getView().findViewById(R.id.text_title);
        text_content = getView().findViewById(R.id.text_content);
        text_top_title.setText(article.getTitle());
        text_title.setText(article.getTitle());
        text_content.setText(article.getContent());
    }

    /**
     * 增加浏览量
     */
    private void addViews() {
        article.setViews(article.getViews() + 1);
        articleDao.update(article);
    }

}
